package com.logic.meeting.core.util.video;

import android.media.MediaPlayer;
import android.view.SurfaceHolder;

import java.io.IOException;

/**
 * Created by honghaonan on 2018/1/8.
 */

public class MediaVideoPlayerUtil {

    // 设置播放不同的视频
    public static void loadSrc(String path, MediaPlayer mMediaPlayer, SurfaceHolder surfaceHolder) {
        // 如果在次播放的是不同的视频，那么就将position初始化,并且reset,重新设置视频源
        try {
            if (mMediaPlayer != null) {
                mMediaPlayer.reset();
                mMediaPlayer.setDataSource(path);
                play(mMediaPlayer, surfaceHolder);
            }
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return;// 中断当前程序

    }

    // 设置播放不同的视频
    public static void loadSrcUnPlay(String path, MediaPlayer mMediaPlayer, SurfaceHolder surfaceHolder) {
        // 如果在次播放的是不同的视频，那么就将position初始化,并且reset,重新设置视频源
        try {
            if (mMediaPlayer != null) {
                mMediaPlayer.reset();
                mMediaPlayer.setDataSource(path);
                play(mMediaPlayer, surfaceHolder);
            }
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return;// 中断当前程序

    }

    // 播放视频
    public static void play(final MediaPlayer mMediaPlayer, final SurfaceHolder surfaceHolder) {
        mMediaPlayer.prepareAsync();
        mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mediaPlayer) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        mMediaPlayer.start();
                    }
                }).start();
            }
        });
    }
}
